<?php

class NewPwdForm extends CFormModel {

    public $newpassword;
    public $newpassword_repeat;
    public $uid;

    public function rules() {
        return array(
            array('newpassword,newpassword_repeat', 'required', 'on' => 'newpwd'),
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            'newpassword' => '新密码',
            'newpassword_repeat' => '验证新密码'
        );
    }

    public function newpwd() {
        if (!$this->hasErrors()) {
            $api = new Api('account');
            $uid = trim($_POST['NewPwdForm']['uid']);
            $newpassword = trim($_POST['NewPwdForm']['newpassword']);
            $newpassword_repeat = trim($_POST['NewPwdForm']['newpassword_repeat']);
            $params = array(
                'UpdatePwdReq' => array(
                    'userId' => $uid,
                    'password' => $newpassword,
                    'confirmPassword' => $newpassword_repeat
                )
            );
            if ($newpassword && $newpassword_repeat) {
                if ($newpassword == $newpassword_repeat) {
                    $row = $api->apiclient('/LdAccountServer/userAccount/updatePwd', $params);
                    if ($row == 0) {
                        throw new CHttpException(404, '网络异常,请稍后再试');
                    }
                    if ($row['code'] == 0) {
                        return true;
                    } else {
                        $this->addError('newpassword', $row['ExceptionRsp']['msg']);
                    }
                } else {
                    $this->addError('newpassword', '二次密码不一致');
                }
            } else {
                $this->addError('newpassword', '请填写新密码');
            }
        }
    }

}

?>
